package saucaca.Adapter;

import java.util.ArrayList;
import java.util.Random;

import saucaca.Object.SongObject;
import saucaca.Service.MusicService;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.PlayActivity;
import com.lemon.kenhnhacvn.R;


public class SongQueueAdapter extends BaseAdapter {
	Context context;
	ArrayList<SongObject> listSongs;
	int songPlaying = -1;
	PopupMenu popupMenu;
	
	CountDownTimer countDownTimer,countDownTimer2,countDownTimer3;
	float fscale1=1f;
	float fscale2=1f;
	float fscale3=1f;
	long duration1=200l;
	long duration2=230l;
	long duration3=260l;
	
	public SongQueueAdapter(Context context, ArrayList<SongObject> listSongs) {
		this.context = context;
		this.listSongs = listSongs;
	}

	@Override
	public int getCount() {
		return listSongs.size();
	}

	public void setListSong(ArrayList<SongObject> listSongs) {
		if (!BaseEngine.compareList(this.listSongs, listSongs)) {
			this.listSongs = listSongs;
			// songPlaying = position;
			notifyDataSetChanged();
		}
	}

	public void setPotitionIndicator(int position) {
		songPlaying = position;
		notifyDataSetChanged();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.item_song_local, null);
			holder.textViewNumber = (TextView) convertView.findViewById(R.id.song_number);
			// holder.img_album=(ImageView)convertView.findViewById(R.id.img_album);
			holder.textViewTitle = (TextView) convertView.findViewById(R.id.song_title);
			holder.textViewArtist = (TextView) convertView.findViewById(R.id.song_artist);
			holder.pop_menu = (ImageButton) convertView.findViewById(R.id.popup_menu);
			
			holder.layout_visual = (LinearLayout) convertView.findViewById(R.id.layout_visual);
			holder.view1 = convertView.findViewById(R.id.visual1);
			holder.view2 = convertView.findViewById(R.id.visual2);
			holder.view3 = convertView.findViewById(R.id.visual3);
//			popupMenu = new PopupMenu(context, holder.pop_menu);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.pop_menu.setVisibility(View.GONE);
		holder.layout = (LinearLayout) convertView.findViewById(R.id.localmusic_linearSong);
		holder.layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				((PlayActivity) context).play(listSongs, position);
				setPotitionIndicator(position);
			}
		});
		if (songPlaying == position) {
			holder.textViewTitle.setTextColor(context.getResources().getColor(R.color.main_color_green));
			holder.textViewNumber.setVisibility(View.GONE);
			holder.layout_visual.setVisibility(View.VISIBLE);
			setVisual(holder.view1, holder.view2, holder.view3);
			if (MusicService.mState.equals(MusicService.MediaPlayerState.Playing)){
					countDownTimer.start();
					countDownTimer2.start();
					countDownTimer3.start();
				}else{
					countDownTimer.cancel();
					countDownTimer2.cancel();
					countDownTimer3.cancel();
					scaleView(holder.view1, fscale1, 0.1f,duration1);
					scaleView(holder.view2, fscale2, 0.1f,duration2);
					scaleView(holder.view3, fscale3, 0.1f,duration3);
				}
		} else {
			holder.textViewNumber.setVisibility(View.VISIBLE);
			 holder.textViewTitle.setTextColor(context.getResources().getColor(R.color.black));
				holder.layout_visual.setVisibility(View.GONE);
		}

		holder.textViewNumber.setText(Integer.toString(position + 1));
		String title = listSongs.get(position).getTXNAME();
		String artist = listSongs.get(position).getNAME_SINGERS();

		holder.textViewTitle.setText(title);
		holder.textViewArtist.setText(artist);
		return convertView;
	}
	
	void setVisual(final View view1,final View view2,final View view3){
		countDownTimer=	 new CountDownTimer(Long.MAX_VALUE, duration1) {
			 Random rand = new Random();	
			 
		     public void onTick(long millisUntilFinished) {
		    	 float f=rand.nextFloat()+0.1f;
		    	 scaleView(view1,fscale1,f,duration1);
		    	 fscale1=f;
		     }

		     public void onFinish() {
		    	 scaleView(view1,fscale1,0.1f,duration1);
		     }
		};
		countDownTimer2 = new CountDownTimer(Long.MAX_VALUE, duration2) {
			Random rand = new Random();

		     public void onTick(long millisUntilFinished) {
		    	 float f=rand.nextFloat()+0.1f;
		    	 scaleView(view2,fscale2,f,duration2);
		    	 fscale2=f;
		     }


			public void onFinish() {
				scaleView(view2, fscale2, 0.1f,duration2);
			}
		};
		countDownTimer3 = new CountDownTimer(Long.MAX_VALUE, duration3) {
			Random rand = new Random();

		     public void onTick(long millisUntilFinished) {
		    	 float f=rand.nextFloat()+0.1f;
		    	 scaleView(view3,fscale3,f,duration3);
		    	 fscale3=f;
		     }
			public void onFinish() {
				scaleView(view3, fscale3, 0.1f,duration3);
			}
		};
	}
	
	
	public void scaleView(View v, float startScale, float endScale, long duration) {
	    Animation anim = 
	    		new ScaleAnimation(
	            1f, 1f, // Start and end values for the X axis scaling
	            startScale, endScale, // Start and end values for the Y axis scaling
	            Animation.RELATIVE_TO_SELF, 1f, // Pivot point of X scaling
	            Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
	   anim.setDuration(duration);
	    anim.setFillAfter(true); // Needed to keep the result of the animation
	    v.startAnimation(anim);
	}
	class ViewHolder {
		LinearLayout layout;
		ImageButton pop_menu;
		TextView textViewNumber;
		TextView textViewTitle;
		TextView textViewArtist;
		ImageView imgeView;
		
		LinearLayout layout_visual;
		View view1;
		View view2;
		View view3;
	}
}
